package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.dto.MediaDTO;
import com.xuecheng.api.media.qo.QueryMedia;
import com.xuecheng.api.media.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MapStructMedia;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService mediaService;

    //媒资信息保存
    @Override
    @PostMapping("media")
    public MediaDTO saveMedia(@RequestBody MediaVO mediaVO) {
        //获得教育机构id
        Long companyId = SecurityUtil.getCompanyId();

        MediaDTO mediaDTO = MapStructMedia.INSTANCE.vo2dto(mediaVO);
        mediaDTO.setCompanyId(companyId);
        return mediaService.saveMedia(mediaDTO);
//        return null;
    }

    @Override
    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams page, @RequestBody QueryMedia media) {
        Long companyId = SecurityUtil.getCompanyId();
        return mediaService.queryMediaList(page, media, companyId);

    }

    //根据媒资id获取浏览地址
    @Override
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        return mediaService.previewMedia(mediaId, companyId);
    }

    @Override
    @DeleteMapping("/media/{mediaId}")
    public void deleteMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        mediaService.deleteMedia(mediaId, companyId);

    }

    @Override
    @GetMapping("l/media/{mediaId}")
    public  RestResponse<MediaDTO>  getMediaById(@PathVariable Long mediaId) {
        return mediaService.getById4Service(mediaId);
    }
}
